package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.Enum.ResultEnum;
import com.dongdongshop.core.SpecificPrint;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.service.EsServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("searchController")
public class SearchController {

    @Reference(check=false ,timeout=1006666)
    private EsServiceI searchService;

    //根据搜索名，查询跳转页
    @RequestMapping("toSearchList")
    public String searchList(String searchInfo, @RequestParam(defaultValue = "0")Integer pageNum, Model model){
        List<TbItem> items = searchService.findSearchInfo(searchInfo,pageNum);
        model.addAttribute("items",items);
        model.addAttribute("searchInfo",searchInfo);
        return "search";
    }

    //分页
    @RequestMapping("searchPage")
    @ResponseBody
    public SpecificPrint searchPage(String searchInfo, @RequestParam(defaultValue = "0")Integer pageNum, Model model){
        try {
            List<TbItem> items = searchService.findSearchInfo(searchInfo,pageNum);
            return SpecificPrint.response(ResultEnum.SUCCESS).setData(items);
        }catch (Exception e){
            return SpecificPrint.response(ResultEnum.ERROR);
        }
    }

}
